package com.ostrich.a2104shopping.ui.home;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.hyphenate.chat.ChatClient;
import com.hyphenate.helpdesk.callback.Callback;
import com.hyphenate.helpdesk.easeui.util.IntentBuilder;
import com.ostrich.a2104shopping.R;
import com.ostrich.a2104shopping.aspectj.LoginCheck;
import com.ostrich.a2104shopping.databinding.ActivityGoodsDetailBinding;
import com.ostrich.a2104shopping.ui.home.adapter.ImageBannerAdapter;
import com.ostrich.a2104shopping.ui.home.entity.AddCarRequestEntity;
import com.ostrich.a2104shopping.ui.home.entity.GoodsDetailEntity;
import com.ostrich.a2104shopping.ui.home.viewmodel.GoodsDetailViewModel;
import com.ostrich.lib_common.base.BaseMvvmActivity;
import com.ostrich.lib_common.net.BaseResponseEntity;
import com.youth.banner.indicator.CircleIndicator;

import java.util.List;

public class GoodsDetailActivity extends BaseMvvmActivity<GoodsDetailViewModel, ActivityGoodsDetailBinding> {
    private  int id;//商品id

    @Override
    public int bindLayout() {
        return R.layout.activity_goods_detail;
    }

    @Override
    public void initView() {
        ChatClient.getInstance().register("yaotianxue", "123456", new Callback(){
            @Override
            public void onSuccess() {
                Log.d("ytx", "注册成功: ");
            }

            @Override
            public void onError(int code, String error) {

            }

            @Override
            public void onProgress(int progress, String status) {

            }
        });

        ChatClient.getInstance().login("yaotianxue", "123456", new Callback(){
            @Override
            public void onSuccess() {
                Log.d("ytx", "登陆成功: ");
            }

            @Override
            public void onError(int code, String error) {

            }

            @Override
            public void onProgress(int progress, String status) {

            }
        });

    }

    @Override
    public void initData() {
        id = getIntent().getExtras().getInt("id", 0);
        viewModel.detail(id).observe(this, new Observer<BaseResponseEntity<GoodsDetailEntity>>() {
            @Override
            public void onChanged(BaseResponseEntity<GoodsDetailEntity> goodsDetailEntityBaseResponseEntity) {
                if(goodsDetailEntityBaseResponseEntity.getCode() == 200){
                    List<String> bannerList = goodsDetailEntityBaseResponseEntity.getData().getBannerList();
                    ImageBannerAdapter imageBannerAdapter = new ImageBannerAdapter(bannerList);
                    binding.banner.setAdapter(imageBannerAdapter)
                            .setBannerGalleryMZ(20)//魅族
//                            .setBannerGalleryEffect(20,5)//画廊

                            .addBannerLifecycleObserver(GoodsDetailActivity.this)//感知生命周期
                            .setIndicator(new CircleIndicator(GoodsDetailActivity.this));
                    //展示商品信息
                    binding.setGoods(goodsDetailEntityBaseResponseEntity.getData());
                }
            }
        });
        //加入购物车
        binding.tvAddCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCar();
            }
        });
        binding.tvCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(CarActivity.class);
            }
        });
        //环信客服
        binding.tvKefu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(ChatClient.getInstance().isLoggedInBefore()){
                    //已经登录，可以直接进入会话界面
                    // 详细参考demo里ChatActivity类，将ChatActivity类和CustomChatFragment类拷贝到自己项目里
// CustomChatFragment类是demo项目扩展功能，继承ChatFragment类，可自行添加修改功能
                    Intent intent = new IntentBuilder(GoodsDetailActivity.this)
                            .setServiceIMNumber("kefuchannelimid_897814") //获取地址：kefu.easemob.com，“管理员模式 > 渠道管理 > 手机APP”页面的关联的“IM服务号”
                            .build();
                    startActivity(intent);
                }else{
                    //未登录，需要登录后，再进入会话界面
                }





            }
        });

    }
    @LoginCheck
    private void addCar() {
        //网络请求
        AddCarRequestEntity addCarRequestEntity = new AddCarRequestEntity();
        addCarRequestEntity.setCount(1);
        addCarRequestEntity.setGoods_id(id);
        viewModel.addCar(addCarRequestEntity).observe(this, new Observer<BaseResponseEntity>() {
            @Override
            public void onChanged(BaseResponseEntity baseResponseEntity) {
                if(baseResponseEntity.getCode() == 200){
                    ToastUtils.showShort("添加购物车成功");
                }else{
                    ToastUtils.showShort("添加购物车失败");
                }
            }
        });




    }

    @Override
    public void initListener() {

    }

    @Override
    public void createViewModel() {
        viewModel = new ViewModelProvider(this).get(GoodsDetailViewModel.class);
    }
}